package dialog;

import java.util.HashMap;
import java.util.Map;

import com.lanhai.javaBean.ControlAreaBean;
import com.lanhai.model.ControlArea;
import com.lanhai.model.TrafficWait;
import com.lanhai.utils.DialogUtils;
import com.lanhai.utils.HexUtils;

import application.Main;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
/**
 * 交管设置窗口
 * @author liangmin
 *
 */
public class TrafficDialog {

//	private static final Logger logger = Logger.getLogger(TrafficDialog.class);
	@FXML
	private ListView<ControlArea> listview;
	@FXML
	private ListView<String> controlAgvs;
	@FXML
	private Button reset;
	private ObservableList<ControlArea> datalist = FXCollections.observableArrayList();
	private ObservableList<String> calist = FXCollections.observableArrayList();
	private Main theMain;
	
	public void init(Main main) {
		theMain = main;
        listview.setItems(datalist);
        loadControlArea();
		
		controlAgvs.setItems(calist);
		loadWaitAgv();
		/**
		 * 释放交管区
		 */
		reset.setOnAction((ActionEvent t) -> {
			ControlArea ca = listview.getSelectionModel().selectedItemProperty().getValue();
			if(ca == null){
				DialogUtils.show("请先选择交管区!", "提示", main.logo);
			}else{
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("path", "submit");//请求路径
				params.put("type", "ca");
				params.put("caId", ca.getId());
				main.httpRequest(params);
			}
			loadControlArea();
			loadWaitAgv();
		});
	}
	
	private void loadControlArea(){
		datalist = FXCollections.observableArrayList();
		for(Map.Entry<Integer, ControlArea> m :theMain.controlAreaMap.entrySet()){;
			datalist.add(m.getValue());
		}	
		listview.setItems(datalist);
	}
	private void loadWaitAgv(){
		calist = FXCollections.observableArrayList();
		theMain.waitAgvList = ControlAreaBean.getTarfficWaitList(theMain.serverUrl);
		for(TrafficWait m:theMain.waitAgvList){
			String caStr = "";
			int[] caArray = m.getControlAreaIds();
			for(int i=0; i<caArray.length; i++){
				caStr += caArray[i];
			}
			calist.add("交管区:" + caStr + ",AGV:" + m.getAgvId() + ",放行指令:" + HexUtils.byte2HexStr(m.getRestartData()));
		}
		controlAgvs.setItems(calist);
	}
	
}
